import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {App, NavController, NavParams, Slides, ToastController} from 'ionic-angular';
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";
import {HuifuPage} from "../huifu/huifu";

/**
 * Generated class for the ZuopininfoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-zuopininfo',
  templateUrl: 'zuopininfo.html',
})
export class ZuopininfoPage {
  /** huifu **/
  huifuMoviesbozhu: Array<any>=[];
  tasklisthuifu:Array<any>=[];//list列表

  leizhuhuifu:Array<any>=[];//list列表
  /** huifu **/
  logintype:string;
  textarea:string;
  zuopinwhere:string="11";
  zuopinid:string;
  /** zuopin **/
  bannerlistitem:Array<any>=[];//list列表
  bannerMoviesvaike: Array<any>=[];
  @ViewChild('slides') slides: Slides;
  /** 具体内容**/
  zuopin_title:string;
  takenAuthor:string;
  authorSchool:string;
  authorAge:string;
  authorTeacher:string;
  takenTool:string;
  content:string;
  createTime:string;
  address:string;
  pohtoTime:string;
  answerOne:string;
  answerTwo:string;
  answerThree:string;
  answerFour:string;
  answerFive:string;
  giveLikeNumber:number;
  replyNumber:string;
  forwardNumber:string;

  constructor(private app:App,public navCtrl: NavController, public navParams: NavParams,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public toastCtrl: ToastController) {
    this.zuopinhttpClinet();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ZuopininfoPage');
  }
  /** 网络请求 **/
  zuopinhttpClinet(){
    this.jidiProvider.getZuoPinInfoHome(this.navParams.get('uuid')).subscribe(data => {
      if(data['success']){
        if(data['data']['worksDetails']['worksImgDetailsList']['length']>0){
          for(let i=0;i<data['data']['worksDetails']['worksImgDetailsList']['length'];i++){
            this.bannerMoviesvaike.push(data['data']['worksDetails']['worksImgDetailsList'][i]);
          }
          //alert(this.jidiMovies);
          this.bannerlistitem=this.bannerMoviesvaike;
          /*this.cdchange.detectChanges();*/
        }
       /** 主体内容**/
       this.zuopin_title = data['data']['worksDetails']['worksTitle'];
       this.takenAuthor =data['data']['worksDetails']['takenAuthor'];
       this.authorSchool =data['data']['worksDetails']['authorSchool'];
       this.authorAge =data['data']['worksDetails']['authorAge'];
       this.authorTeacher =data['data']['worksDetails']['authorTeacher'];
       this.takenTool =data['data']['worksDetails']['takenTool'];
       this.content = data['data']['worksDetails']['content'];
       this.createTime = data['data']['worksDetails']['createTime'];
       this.address = data['data']['worksDetails']['address'];
       this.pohtoTime = data['data']['worksDetails']['pohtoTime'];
       this.answerOne = data['data']['worksDetails']['answerOne'];
       this.answerTwo = data['data']['worksDetails']['answerTwo'];
       this.answerThree = data['data']['worksDetails']['answerThree'];
       this.answerFour = data['data']['worksDetails']['answerFour'];
       this.answerFive = data['data']['worksDetails']['answerFive'];

       this.giveLikeNumber = data['data']['worksDetails']['giveLikeNumber'];
       this.replyNumber = data['data']['worksDetails']['replyNumber'];
       this.forwardNumber = data['data']['worksDetails']['forwardNumber'];
        this.zuopinwhere=data['data']['worksDetails']['id'];
        /** 回复列表请求 **/
        this.huifuListEvent();

      }else{
        localStorage.setItem('login',"no");
        //this.app.getRootNav().push(LoginPage);
      }
    },error => {

    });

  }
  /** 作品详情 banner **/
  ionViewDidEnter() {
    if(this.slides){

      this.slides.autoplayDisableOnInteraction = false;
    }
  }

  /** 点赞事件 **/
  dianZanEvent(){
    this.logintype =localStorage.getItem('login');
    if("no"==this.logintype){
      this.showToast("请登陆后操作！");
    }else{
      this.jidiProvider.dianZanClick(this.zuopinwhere).subscribe(data => {
        if(data['success']){
          this.showToast(data['message']);
          this.giveLikeNumber = ++this.giveLikeNumber;
          this.cdchange.detectChanges();
        }else{
          this.showToast(data['message']);
        }

      },error => {
        this.showToast("投票失败");
      });
    }


  }

  /** 回复事件 **/
  huiFuEvent(){
    this.logintype =localStorage.getItem('login');
    if("no"==this.logintype){
      this.showToast("请登陆后操作！");
    }else{
      this.app.getRootNav().push(HuifuPage,{uuid:this.zuopinwhere});
    }


  }

  /** toast **/
  /** 提示信息 **/
  showToast(position: string) {
    let toast = this.toastCtrl.create({
      message: position,
      duration: 2000,
      position: "middle"
    });

    toast.present(toast);
  }

  /** **/
  pinglunEvent(){
    this.logintype =localStorage.getItem('login');
    if("no"==this.logintype){
      this.showToast("请登陆后操作！");
    }else{
      this.jidiProvider.pingLunMessage(this.zuopinwhere,this.textarea).subscribe(data => {
        if(data['success']){
          this.showToast(data['message']);
        }else{
          this.showToast(data['message']);
        }



      },error => {
        this.showToast("error");
      });
    }
    //alert(this.textarea)

  }

  /** 回复事件请求 **/
  huifuListEvent(){

    this.jidiProvider.huiFuClick(this.zuopinwhere).subscribe(data => {
      if(data['data']['length']>0){
        for(let i=0;i<data['data']['length'];i++){
          this.huifuMoviesbozhu.push(data['data'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklisthuifu=this.huifuMoviesbozhu;
        this.cdchange.detectChanges();
      }
    },error => {

    });
    this.jidiProvider.leizhuhuiFuClick(this.zuopinwhere).subscribe(data => {
      if(data['data']['length']>0){
        for(let i=0;i<data['data']['length'];i++){
          this.leizhuhuifu.push(data['data'][i]);
        }
        //alert(this.jidiMovies);
        this.cdchange.detectChanges();
      }
    },error => {

    });
  }




}
